//
//  BuyDetailTableViewController.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-9-29.
//
//

#import "JddBuyDetailTableViewController.h"
#import "JddFootBallBuyItemCell.h"
#import "JddDigitalBuyItemCell.h"
#import "JddDigitalBuyItemCell.h"
#import "JddSportBuyItemCell.h"
#import "JddBasketBallBuyItemCell.h"
#import "JddDefine.h"
@interface JddBuyDetailTableViewController ()

@end

@implementation JddBuyDetailTableViewController

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *ssqNumber = [self.tableNumberList objectAtIndex:indexPath.row];
    if (self.lottId == LOTTERY_TYPE_ID_BD)//北单
        {
        JddSportBuyItemCell *cell = [self getBDSportBuyItemCellWithssNumber:ssqNumber];
        return 44+cell.content.frame.size.height;
        }
    else if(self.lottId == LOTTERY_TYPE_ID_FOOTBALL){
        if (self.playTypeID == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
            JddFootBallBuyItemCell *cell = [self getFootBallBuyItemCellWithssqNumber:ssqNumber];
            return 78+cell.content.frame.size.height;
        }else{
            JddSportBuyItemCell *cell = [self getSportBuyItemCellWithssNumber:ssqNumber];
            return 44+cell.content.frame.size.height;;
        }
    }
    else if (self.lottId == LOTTERY_TYPE_ID_BASKETBALL) {
        JddBasketBallBuyItemCell *cell;
        if (self.playTypeID == LOTTERY_PLAY_METHOD_BASKETBALL_HH) {
            cell = [self getHHBasketBallBuyItemCellWithssqNumber:ssqNumber];
            return 70+cell.content.frame.size.height;
        }else{
            cell = [self getBasketBallBuyItemCellWithssqNumber:ssqNumber];
            int height = 65;
            if (self.playTypeID == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
                height = 85;
            }
            return height+cell.content.frame.size.height;
        }
    }
    else if(self.sportType == 0){
        JddDigitalBuyItemCell *cell = [self getDigitalBuyItemCellWithnumberCaiDic:ssqNumber];
        return 20+cell.number.frame.size.height;
    }
    return 44.0;
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.tableNumberList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *ssqNumber = [self.tableNumberList objectAtIndex:indexPath.row];
//    NSLog(@"%@", ssqNumber);
    //数字彩
    if (self.sportType==0) {
        JddDigitalBuyItemCell *cell = [self getDigitalBuyItemCellWithnumberCaiDic:ssqNumber];
        return cell;
    }
    else{
        //足球单场
        if (self.lottId == LOTTERY_TYPE_ID_BD) {
            JddSportBuyItemCell *cell = [self getBDSportBuyItemCellWithssNumber:ssqNumber];
            return cell;
        }else
            {
            //足球
            if (self.lottId == LOTTERY_TYPE_ID_FOOTBALL) {
                //竞彩足球 混合
                if (self.playTypeID == LOTTERY_PLAY_METHOD_FOOTBALL_HH) {
                    JddFootBallBuyItemCell *cell = [self getFootBallBuyItemCellWithssqNumber:ssqNumber];
                    return cell;
                }else{
                    JddSportBuyItemCell *cell = [self getSportBuyItemCellWithssNumber:ssqNumber];
                    return cell;
                }
                //篮球
            }else{
                //篮球 混合
                if (self.playTypeID == LOTTERY_PLAY_METHOD_BASKETBALL_HH) {
                    JddBasketBallBuyItemCell *cell = [self getHHBasketBallBuyItemCellWithssqNumber:ssqNumber];
                    return cell;
                }else{
                    JddBasketBallBuyItemCell *cell = [self getBasketBallBuyItemCellWithssqNumber:ssqNumber];
                    return cell;
                }
            }
            }
    }
}


//竞彩足球 混合
-(JddFootBallBuyItemCell *)getFootBallBuyItemCellWithssqNumber:(NSDictionary *)ssqNumber
{
    JddFootBallBuyItemCell *cell = [JddFootBallBuyItemCell getInstanceView];
    cell.mno.text = [ssqNumber objectForKey:@"no"];
    NSString *result = [ssqNumber objectForKey:@"content"];
////    cell.content.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:result AndCellLabel:cell.content];
//    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[result dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//    cell.content.attributedText = attrStr;

//    [self getHeightWithLable:cell.content];
    result = [result stringByReplacingOccurrencesOfString:@"<ul class='football_ulwf'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</ul>" withString:@""];
    NSArray *results = [result componentsSeparatedByString:@"</li><li>"];
    NSString *str1 = @"";
    NSString *strtemp1 = @"";
    for (int i=0; i<[results count]; i++) {
        strtemp1 = [[results objectAtIndex:i] stringByReplacingOccurrencesOfString:@"<li>" withString:@""];
        strtemp1 = [strtemp1 stringByReplacingOccurrencesOfString:@"</li>" withString:@""];
        if(i == 0){
            str1 = [NSString stringWithFormat:@"%@",strtemp1];
        }else{
            str1 = [NSString stringWithFormat:@"%@\n%@",str1,strtemp1];
        }
    }
    cell.content.text = str1;
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[result dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    cell.content.attributedText = attrStr;
//    [self reSetHeightWithLabel: cell.content];
    [self getHeightWithLable:cell.content];
    
    
    cell.HTeam.text = [ssqNumber objectForKey:@"hteam"];
    cell.VTeam.text = [ssqNumber objectForKey:@"vteam"];
//    cell.hScore.text = [ssqNumber objectForKey:@"hscore"];
//    cell.score.text = [ssqNumber objectForKey:@"score"];
    
    NSString * score = [ssqNumber objectForKey:@"score"];
    score = [score stringByReplacingOccurrencesOfString:@"<ul class='football_dd'>" withString:@""];
    score = [score stringByReplacingOccurrencesOfString:@"</ul>" withString:@""];
    NSArray *scoress = [score componentsSeparatedByString:@"</li><li>"];
    NSLog(@"%d",[scoress count]);
    NSString *str = @"";
    NSString *strtemp = @"";
    for (int i=0; i<[scoress count]; i++) {
        strtemp = [[scoress objectAtIndex:i] stringByReplacingOccurrencesOfString:@"<li>" withString:@""];
        strtemp = [strtemp stringByReplacingOccurrencesOfString:@"</li>" withString:@""];
        if(i == 0){
            str = [NSString stringWithFormat:@"%@",strtemp];
        }else{
            str = [NSString stringWithFormat:@"%@\n%@",str,strtemp];
        }
    }
    cell.label_scr.text = str;
//    [self reSetHeightWithLabel: cell.label_scr];
    return cell;
}
//数字彩
-(JddDigitalBuyItemCell *)getDigitalBuyItemCellWithnumberCaiDic:(NSDictionary *)numberCaiDic
{
    JddDigitalBuyItemCell *cell = [JddDigitalBuyItemCell getInstanceView] ;
    NSString * LotNumber = [numberCaiDic objectForKey:@"LotNumber"];
    NSArray *numbers = [LotNumber componentsSeparatedByString:@";"];
    NSMutableString *redBalls = [NSMutableString string];
    NSMutableString *blueBalls = [NSMutableString string];
    for(NSString *s in numbers ){
        NSArray *rbs = [s componentsSeparatedByString:@"+"];
        [redBalls appendFormat:@"%@\n", [rbs objectAtIndex:0]];
        if (rbs.count > 1) {
            [blueBalls appendFormat:@"%@\n", [rbs objectAtIndex:1]];
        }
    }
    [redBalls insertString:@"\n" atIndex:0];

    if (blueBalls.length > 2) {
        [blueBalls insertString:@"\n" atIndex:0];
    }

    cell.number.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:redBalls AndCellLabel:cell.number];
    cell.number2.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:blueBalls AndCellLabel:cell.number2];
    [cell.number setAutomaticallyAddLinksForType:0];
    [cell.number2 setAutomaticallyAddLinksForType:0];
    
    [self getHeightWithLable:cell.number];
    [self getHeightWithLable:cell.number2];
    
    cell.playTypeName.text = [numberCaiDic objectForKey:@"PlayTypeName"];
    return cell;
}
//竞彩足球 非混合
-(JddSportBuyItemCell *)getSportBuyItemCellWithssNumber:(NSDictionary *)ssqNumber
{
    JddSportBuyItemCell *cell = [JddSportBuyItemCell getInstanceView];
    cell.rq.hidden = YES;
    cell.rgorrfLabel.hidden = YES;
    cell.bfLabel.frame = cell.rgorrfLabel.frame;
    cell.score.frame = cell.rq.frame;
    cell.mno.text = [ssqNumber objectForKey:@"no"];
    NSString *cg = [ssqNumber objectForKey:@"cg"];
    //赛果转换
    cg = [JddCommonFunction getCgWithFangAnCg:cg AndPlayTypeName:self.playTypeName];
    cell.cg.text = cg;
    cell.score.text = [ssqNumber objectForKey:@"score"];
    NSString *result = [ssqNumber objectForKey:@"content"];
//    cell.content.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:result AndCellLabel:cell.content];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[result dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    cell.content.attributedText = attrStr;
    
    cell.content.text = result;
    cell.VTeam.text = [ssqNumber objectForKey:@"vteam"];
    NSString *ht = [ssqNumber objectForKey:@"hteam"];
    //竞彩足球 让球胜平负
    if (self.playTypeID == LOTTERY_PLAY_METHOD_FOOTBALL) {
        ht = [ht stringByAppendingFormat:@"(%@)",[ssqNumber objectForKey:@"rq"]];
    }
    cell.HTeam.text = ht;
    [self getHeightWithLable:cell.content];
    return cell;
}
//足球单场
-(JddSportBuyItemCell *)getBDSportBuyItemCellWithssNumber:(NSDictionary *)ssqNumber
{
    JddSportBuyItemCell *cell = [JddSportBuyItemCell getInstanceView];
    cell.mno.text = [ssqNumber objectForKey:@"no"];
    NSString *cg = [ssqNumber objectForKey:@"cg"];
    //赛果转换
    cg = [JddCommonFunction getCgWithFangAnCg:cg AndPlayTypeName:self.playTypeName];
    cell.cg.text = cg;
    cell.score.text = [ssqNumber objectForKey:@"score"];
    NSString *result = [ssqNumber objectForKey:@"content"];
    
    NSString *str = [ssqNumber objectForKey:@"vs"];
    NSArray *teams = [str componentsSeparatedByString:@"VS"];
    if([teams count]>1){
        cell.HTeam.text = [teams objectAtIndex:0];
        cell.VTeam.text = [teams objectAtIndex:1];
        cell.rq.text = @"0";
    }else{
        NSRange l = [str rangeOfString:@"("];
        NSRange r = [str rangeOfString:@")"];
        NSString *range = [NSString stringWithFormat:@"%d, %d", l.location+1, r.location-l.location-1];
        NSString *rq = [str substringWithRange:NSRangeFromString(range)];
        NSString *ht = [str substringToIndex:l.location];
        NSString *vt = [str substringFromIndex:r.location+1];
        
        cell.HTeam.text = ht;
        cell.VTeam.text = vt;
        cell.rq.text = rq;
    }
    cell.content.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:result AndCellLabel:cell.content];
    [self getHeightWithLable:cell.content];
    return cell;
}
//篮球 混合
-(JddBasketBallBuyItemCell *)getHHBasketBallBuyItemCellWithssqNumber:(NSDictionary *)ssqNumber
{
    JddBasketBallBuyItemCell *cell = [JddBasketBallBuyItemCell getInstanceView];
    //隐藏赛果，预设分，替换比分
    cell.cgLabel.hidden = YES;
    cell.cg.hidden = YES;
    cell.ysLabel.hidden = YES;
    cell.ys.hidden = YES;
    cell.scroeLabel.frame = cell.ysLabel.frame;
    cell.score.frame = cell.ys.frame;
    cell.mno.text = [ssqNumber objectForKey:@"no"];
    NSString *result = [ssqNumber objectForKey:@"content"];
    //投注到赛果的位置上
    CGRect contentFrame = cell.content.frame;
    contentFrame.origin.y = cell.cg.frame.origin.y;
    [cell.content setFrame:contentFrame];
    
    CGRect contentLblFrame = cell.contentLbl.frame;
    contentLblFrame.origin.y = cell.cg.frame.origin.y;
    [cell.contentLbl setFrame:contentLblFrame];
    
//    cell.content.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:result AndCellLabel:cell.content];
//    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[result dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
//    cell.content.attributedText = attrStr;
//    [self getHeightWithLable:cell.content];
    result = [result stringByReplacingOccurrencesOfString:@"<ul class='football_ulwf'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</ul>" withString:@""];
    NSArray *results = [result componentsSeparatedByString:@"</li><li>"];
    NSString *str1 = @"";
    NSString *strtemp1 = @"";
    for (int i=0; i<[results count]; i++) {
        strtemp1 = [[results objectAtIndex:i] stringByReplacingOccurrencesOfString:@"<li>" withString:@""];
        strtemp1 = [strtemp1 stringByReplacingOccurrencesOfString:@"</li>" withString:@""];
        if(i == 0){
            str1 = [NSString stringWithFormat:@"%@",strtemp1];
        }else{
            str1 = [NSString stringWithFormat:@"%@\n%@",str1,strtemp1];
        }
    }
    cell.content.text = str1;
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[result dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    cell.content.attributedText = attrStr;
//    cell.content.text = result;
    [self reSetHeightWithLabel: cell.content];
    
    cell.HTeam.text = [ssqNumber objectForKey:@"hteam"];
    cell.VTeam.text = [ssqNumber objectForKey:@"vteam"];
    cell.score.text = [ssqNumber objectForKey:@"score"];
    return cell;
}

//篮球 非混合
-(JddBasketBallBuyItemCell *)getBasketBallBuyItemCellWithssqNumber:(NSDictionary *)ssqNumber
{
    JddBasketBallBuyItemCell *cell = [JddBasketBallBuyItemCell getInstanceView];
    cell.mno.text = [ssqNumber objectForKey:@"no"];
    cell.cg.text = [ssqNumber objectForKey:@"cg"];
    cell.score.text = [ssqNumber objectForKey:@"score"];
    NSString *result = [ssqNumber objectForKey:@"content"];
    
    result = [result stringByReplacingOccurrencesOfString:@"<span style='color:red;'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</span>" withString:@""];
    
    
//    result = [result stringByReplacingOccurrencesOfString:@"<font color=\"#a50000\">" withString:@""];
//    result = [result stringByReplacingOccurrencesOfString:@"<font color='red'>" withString:@""];
//    result = [result stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    
    
    
    
//    cell.content.attributedText = [JddCommonFunction convertHTMLFontRedWithResult:result AndCellLabel:cell.content];
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[result dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    cell.content.attributedText = attrStr;
    cell.content.text = result;
    NSString *ht = [ssqNumber objectForKey:@"hteam"];
    NSString *vt = [ssqNumber objectForKey:@"vteam"];
    //篮球 让分胜负
    if (self.playTypeID == LOTTERY_PLAY_METHOD_BASKETBALL_RFSF) {
//        NSString *rf = [ssqNumber objectForKey:@"rf"];
//        if ([rf rangeOfString:@"-"].location != NSNotFound) {
//            ht = [ht stringByAppendingFormat:@"(%@)",rf];
//        }else{
//            ht = [ht stringByAppendingFormat:@"(+%@)",rf];
//        }
        
        
        NSString * rff = [ssqNumber objectForKey:@"vs"];
//        费城76人 VS 印第安那步行者(-14.5)
        NSArray* arr = [rff componentsSeparatedByString:@"("];
        if ([arr count] > 1) {
            NSString *RFFf = [arr objectAtIndex: ([arr count]-1)];
            RFFf = [RFFf substringToIndex:([RFFf length]-1)];
                    if ([RFFf rangeOfString:@"-"].location != NSNotFound) {
                        ht = [ht stringByAppendingFormat:@"(%@)",RFFf];
                    }else{
                        ht = [ht stringByAppendingFormat:@"(+%@)",RFFf];
                    }
        }
        
    }
    if (self.playTypeID == LOTTERY_PLAY_METHOD_BASKETBALL_DXF) {
        //显示预设分
        cell.ysLabel.hidden = NO;
        cell.ys.hidden = NO;
        cell.ys.text = [ssqNumber objectForKey:@"pre"];
    }else{
        //隐藏预设分
        cell.ysLabel.hidden = YES;
        cell.ys.hidden = YES;
        CGRect cgFrame = cell.cg.frame;
        
        //移动投注到赛果的y
        CGRect contentLblFrame = cell.contentLbl.frame;
        contentLblFrame.origin.y = cgFrame.origin.y;
        [cell.contentLbl setFrame:contentLblFrame];
        
        CGRect contentFrame = cell.content.frame;
        contentFrame.origin.y = cgFrame.origin.y;
        [cell.content setFrame:contentFrame];
        
        //移动赛果和比分的y到预设分位置
        CGFloat ysFrameY = cell.ys.frame.origin.y;
        cgFrame.origin.y = ysFrameY;
        [cell.cg setFrame:cgFrame];
        CGRect cgLblFrame = cell.cgLabel.frame;
        cgLblFrame.origin.y = ysFrameY;
        [cell.cgLabel setFrame:cgLblFrame];
        
        CGRect scoreFrame = cell.score.frame;
        scoreFrame.origin.y = ysFrameY;
        [cell.score setFrame:scoreFrame];
        CGRect scoreLblFrame = cell.scroeLabel.frame;
        scoreLblFrame.origin.y = ysFrameY;
        [cell.scroeLabel setFrame:scoreLblFrame];
    }
    cell.VTeam.text = vt;
    cell.HTeam.text = ht;
    [self getHeightWithLable:cell.content];
    
    return cell;
}
-(void)reSetHeightWithLabel:(UILabel *) lbl{
    lbl.numberOfLines = 0;
    CGRect frame = lbl.frame;
    UIFont *font = lbl.font;
    CGFloat width = frame.size.width;
    NSString *result = lbl.text;
//    lbl.lineBreakMode = NSLineBreakByWordWrapping;
    result = [result stringByReplacingOccurrencesOfString:@"<ul class='football_ulwf'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</ul>" withString:@""];
    NSArray *resultsss = [result componentsSeparatedByString:@"</li><li>"];
    NSString *str = @"";
    NSString *strtemp = @"";
    float adHeig = 0;
    for (int i=0; i<[resultsss count]; i++) {
        strtemp = [[resultsss objectAtIndex:i] stringByReplacingOccurrencesOfString:@"<li>" withString:@""];
        strtemp = [strtemp stringByReplacingOccurrencesOfString:@"</li>" withString:@""];
        
        CGSize size = [result sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT)lineBreakMode:NSLineBreakByWordWrapping];
        
        adHeig = adHeig + size.height;
        if(i == 0){
            str = [NSString stringWithFormat:@"%@",strtemp];
        }else{
            str = [NSString stringWithFormat:@"%@\n%@",str,strtemp];
        }
    }
    NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    lbl.attributedText = attrStr;
//    CGRect bounds;
//    bounds = CGRectMake(0, 0,width, 300);
//    CGRect heightLabel = [lbl textRectForBounds:bounds limitedToNumberOfLines:[resultsss count]];
//    frame.size.height = heightLabel.size.height;
    
    frame.size.height = adHeig + 10;
    [lbl setFrame:frame];
}
-(void)getHeightWithLable:(UILabel *) lbl
{
    lbl.numberOfLines = 0;
    CGRect frame = lbl.frame;
    UIFont *font = lbl.font;
    CGFloat width = frame.size.width;
    NSString *result = lbl.text;
    lbl.lineBreakMode = NSLineBreakByWordWrapping;
    //替换html使不影响计算高度
    result = [result stringByReplacingOccurrencesOfString:@"<font color=\"#a50000\">" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"<font color='red'>" withString:@""];
    result = [result stringByReplacingOccurrencesOfString:@"</font>" withString:@""];
    CGSize size = [result sizeWithFont:font constrainedToSize:CGSizeMake(width, MAXFLOAT)lineBreakMode:NSLineBreakByWordWrapping];
    frame.size.height = size.height;
    if(IOS7){
        frame.size.height = size.height + 20;
    }
    NSLog(@"%@",lbl.text);
    [lbl setFrame:frame];
}
@end
